package com.ossjk.qlh.system.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.loginfo.LogModule;
import com.ossjk.qlh.loginfo.LogOperation;
import com.ossjk.qlh.system.entity.Department;
import com.ossjk.qlh.system.service.IDepartmentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author chair
 * @since 2020-02-06
 */
@LogModule(value = "部门管理")
@Api(tags = "部门管理")
@RestController
@RequestMapping("/system/department")
public class DepartmentController extends BaseController {

	@Autowired
	private IDepartmentService iDepartmentService;

	@LogOperation("列表")
	@ApiOperation(value = "列表")
	@PostMapping(value = "/list")
	@RequiresPermissions("00000020")
	public ResponseBean<List<Department>> list() {
		QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
		queryWrapper.orderByAsc("level", "sort");
		return responseSuccess(iDepartmentService.list(queryWrapper));
	}

	@LogOperation("列表")
	@ApiOperation(value = "去编辑")
	@PostMapping(value = "/toUpdate")
	@RequiresPermissions("000000200005")
	public ResponseBean<Department> toUpdate(@RequestBody RequestBean requestBean) {
		Department department = iDepartmentService.getById((String) requestBean.getParam().get("id"));
		if (!CommonUtil.isBlank(department)) {
			return responseSuccess(department);
		} else {
			return responseFail();
		}
	}

	@LogOperation("添加")
	@ApiOperation(value = "添加")
	@PostMapping(value = "/save")
	@RequiresPermissions("000000200000")
	public ResponseBean save(@RequestBody RequestBean<Department> requestBean) {
		if (iDepartmentService.save(requestBean.getData())) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@LogOperation("编辑")
	@ApiOperation(value = "编辑")
	@PostMapping(value = "/update")
	@RequiresPermissions("000000200005")
	public ResponseBean udate(@RequestBody RequestBean<Department> requestBean) {
		if (iDepartmentService.updateById(requestBean.getData())) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@LogOperation("删除")
	@ApiOperation(value = "删除")
	@PostMapping(value = "/remove")
	@RequiresPermissions("000000200010")
	public ResponseBean delete(@RequestBody RequestBean requestBean) {
		String id = (String) requestBean.getParam().get("id");
		// 删除子节点
		if (iDepartmentService.removeChildrenById(id)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
}
